package setting

import (
	"context"
	"errors"

	"gitee.com/liuxuezhan/ar-platform/admin-api/internal/svc"
	"gitee.com/liuxuezhan/ar-platform/admin-api/internal/types"

	pb "gitee.com/liuxuezhan/ar-platform/rpc-client/safeBoxClient/rpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type UpdateSafeConfigLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

// 更新保险箱配置信息
func NewUpdateSafeConfigLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateSafeConfigLogic {
	return &UpdateSafeConfigLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *UpdateSafeConfigLogic) UpdateSafeConfig(req *types.UpdateSafeConfigReq) (resp *types.UpdateSafeConfigResp, err error) {
	// 验证参数
	if req.Quota <= 0 {
		return nil, errors.New("Quota must be greater than 0")
	}
	if req.Rate <= 0 {
		return nil, errors.New("Rate must be greater than 0")
	}
	// RPC请求
	in := &pb.UpdateSafeConfigRequest{
		Quota: req.Quota,
		Rate:  req.Rate,
	}
	out, err := l.svcCtx.SafeBoxRpcService.Setting.UpdateSafeConfig(l.ctx, in)
	if err != nil {
		return nil, err
	}
	return &types.UpdateSafeConfigResp{Quota: out.Quota, Rate: out.Rate}, nil
}
